package com.wwj.springcloud.controller;

import com.wwj.springcloud.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptController {

//    private static final String URL_PREFIX = "http://localhost:8001";
    // ribbon，对应springcloud-provider-dept-8001的yml中spring.application.name
    private static final String URL_PREFIX = "http://springcloud-provider-dept";

    @Autowired
    private RestTemplate restTemplate;

    // http://localhost/customer/dept/add?name=wwj&dbName=2233
    @RequestMapping("/customer/dept/add")
    public boolean add(Dept dept) {
        System.out.println("-------------");
        System.out.println(dept);
        System.out.println("-------------");
        return restTemplate.postForObject(URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

    // http://localhost/customer/dept/get/2
    @RequestMapping("/customer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return restTemplate.getForObject(URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    // http://localhost/customer/dept/getAllDept
    @RequestMapping("/customer/dept/getAllDept")
    public List<Dept> getAll() {
        return restTemplate.getForObject(URL_PREFIX + "/dept/getAllDept", List.class);
    }


}
